//
//  AuthenCompanySettleVM.swift
//  kxf
//
//  Created by Mac on 2024/3/8.
//

import Foundation
import UIKit
import MCToast

class AuthenCompanySettleVM: UIView {
    
    var selfHeight = kFitWidth(10)
    
    var msgVmOriginY = kFitWidth(0)
    
    var nextBlock:(()->())?
    
    override init(frame:CGRect){
        super.init(frame: CGRect.init(x: 0, y: frame.origin.y, width: SCREEN_WIDHT, height: frame.size.height))
        self.selfHeight = frame.size.height
        self.backgroundColor = .COLOR_LIGHT_GREY
        self.isUserInteractionEnabled = true
        
        initUI()
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    lazy var scrollView : UIScrollView = {
        let scro = UIScrollView.init(frame: CGRect.init(x: 0, y: 0, width: SCREEN_WIDHT, height: self.selfHeight))
        scro.backgroundColor = .COLOR_LIGHT_GREY
        if #available(iOS 11.0, *) {
            scro.contentInsetAdjustmentBehavior = .never
        } else {
            // Fallback on earlier versions
        }
        scro.bounces = true
        
        return scro
    }()
    lazy var photoVm : AuthenCompanySettlePhotoVM = {
        let vm = AuthenCompanySettlePhotoVM.init(frame: CGRect.init(x: 0, y: 0, width: 0, height: 0))
        vm.bankOcrFrontBlock = {(dict)in
            self.udpateBankCardBackMsg(bankMsgDict:dict)
        }
        return vm
    }()
    lazy var msgVm : AuthenCompanySettleMsgVM = {
        let vm = AuthenCompanySettleMsgVM.init(frame: CGRect.init(x: 0, y: self.photoVm.frame.maxY+kFitWidth(10), width: 0, height: 0))
        self.msgVmOriginY = self.photoVm.frame.maxY+kFitWidth(10)
        vm.bankTypeChangeBlock = {(dict)in
            if dict["type"]as? String ?? "" == "1"{
                self.photoVm.bankCardPhotoVm.isHidden = true
                self.photoVm.bankXuKePhotoVm.isHidden = false
                UIView.animate(withDuration: 0.3, delay: 0) {
                    if AuthenCompanyMsgModel.shareInstance.settleWay == "LEGAL_PERSON"{
                        self.msgVm.frame = CGRect.init(x: 0, y: self.msgVmOriginY, width: SCREEN_WIDHT, height: RegisterViewHeadTitleVM().selfHeight+RegisterInputVM().selfHeight*5)
                        self.nextBtn.frame = CGRect.init(x: kFitWidth(31), y: self.msgVmOriginY+RegisterViewHeadTitleVM().selfHeight+RegisterInputVM().selfHeight*5+kFitWidth(40), width: kFitWidth(316), height: kFitWidth(48))
                    }else{
                        self.msgVm.frame = CGRect.init(x: 0, y: self.msgVmOriginY, width: SCREEN_WIDHT, height: RegisterViewHeadTitleVM().selfHeight+RegisterInputVM().selfHeight*2)
                        self.nextBtn.frame = CGRect.init(x: kFitWidth(31), y: self.msgVmOriginY+RegisterViewHeadTitleVM().selfHeight+RegisterInputVM().selfHeight*2+kFitWidth(40), width: kFitWidth(316), height: kFitWidth(48))
                    }
                }
            }else{
                self.photoVm.bankCardPhotoVm.isHidden = false
                self.photoVm.bankXuKePhotoVm.isHidden = true
                UIView.animate(withDuration: 0.3, delay: 0) {
                    if AuthenCompanyMsgModel.shareInstance.settleWay == "LEGAL_PERSON"{
                        self.msgVm.frame = CGRect.init(x: 0, y: self.msgVmOriginY, width: SCREEN_WIDHT, height: RegisterViewHeadTitleVM().selfHeight+RegisterInputVM().selfHeight*6)
                        self.nextBtn.frame = CGRect.init(x: kFitWidth(31), y: self.msgVmOriginY+RegisterViewHeadTitleVM().selfHeight+RegisterInputVM().selfHeight*6+kFitWidth(40), width: kFitWidth(316), height: kFitWidth(48))
                    }else{
                        self.msgVm.frame = CGRect.init(x: 0, y: self.msgVmOriginY, width: SCREEN_WIDHT, height: RegisterViewHeadTitleVM().selfHeight+RegisterInputVM().selfHeight*2)
                        self.nextBtn.frame = CGRect.init(x: kFitWidth(31), y: self.msgVmOriginY+RegisterViewHeadTitleVM().selfHeight+RegisterInputVM().selfHeight*2+kFitWidth(40), width: kFitWidth(316), height: kFitWidth(48))
                    }
                }
            }
//            self.photoVm.updateUIForFYPublic()
            self.scrollView.contentSize = CGSize.init(width: 0, height: self.nextBtn.frame.maxY+kFitWidth(20))
            
            if AuthenCompanyMsgModel.shareInstance.settleWay == "LEGAL_PERSON"{
                self.refreshFrameForUnincorporateVM(isShowUnincorporate: false)
            }else{
                self.refreshFrameForUnincorporateVM(isShowUnincorporate: true)
            }
        }
        vm.settleTypeChangeBlock = {(dict)in
            if AuthenCompanyMsgModel.shareInstance.settleWay == "LEGAL_PERSON"{
                if AuthenCompanyMsgModel.shareInstance.settleBankType == "1"{//对公
                    self.msgVm.frame = CGRect.init(x: 0, y: self.msgVmOriginY, width: SCREEN_WIDHT, height: RegisterViewHeadTitleVM().selfHeight+RegisterInputVM().selfHeight*5)
                    self.nextBtn.frame = CGRect.init(x: kFitWidth(31), y: self.msgVmOriginY+RegisterViewHeadTitleVM().selfHeight+RegisterInputVM().selfHeight*5+kFitWidth(40), width: kFitWidth(316), height: kFitWidth(48))
                }else{
                    self.msgVm.frame = CGRect.init(x: 0, y: self.msgVmOriginY, width: SCREEN_WIDHT, height: RegisterViewHeadTitleVM().selfHeight+RegisterInputVM().selfHeight*6)
                    self.nextBtn.frame = CGRect.init(x: kFitWidth(31), y: self.msgVmOriginY+RegisterViewHeadTitleVM().selfHeight+RegisterInputVM().selfHeight*6+kFitWidth(40), width: kFitWidth(316), height: kFitWidth(48))
                }
                
                self.refreshFrameForUnincorporateVM(isShowUnincorporate: false)
            }else{
                self.msgVm.frame = CGRect.init(x: 0, y: self.msgVmOriginY, width: SCREEN_WIDHT, height: RegisterViewHeadTitleVM().selfHeight+RegisterInputVM().selfHeight*2)
                self.nextBtn.frame = CGRect.init(x: kFitWidth(31), y: self.msgVmOriginY+RegisterViewHeadTitleVM().selfHeight+RegisterInputVM().selfHeight*2+kFitWidth(40), width: kFitWidth(316), height: kFitWidth(48))
                self.refreshFrameForUnincorporateVM(isShowUnincorporate: true)
            }
        }
        return vm
    }()
    lazy var unincorporateVM: UnincorporateVM = {
        let vm = UnincorporateVM.init(frame: CGRect.init(x: 0, y: self.msgVm.frame.maxY+kFitWidth(8), width: 0, height: 0))
        vm.isHidden = true
        return vm
    }()
    lazy var nextBtn : UIButton = {
        let btn = UIButton()
        btn.frame = CGRect.init(x: kFitWidth(31), y: self.msgVm.frame.maxY+kFitWidth(40), width: kFitWidth(316), height: kFitWidth(48))
        btn.setTitle("保存并下一步", for: .normal)
        btn.setTitleColor(.white, for: .normal)
        btn.setBackgroundImage(UIImage.init(named: "button_theme_bg"), for: .normal)
        btn.layer.cornerRadius = kFitWidth(24)
        btn.clipsToBounds = true
        btn.titleLabel?.font = .systemFont(ofSize: 16, weight: .regular)
        btn.backgroundColor = .clear
        
        btn.addTarget(self, action: #selector(nextAction), for: .touchUpInside)
        
        return btn
    }()
}

extension AuthenCompanySettleVM{
    func initUI(){
        addSubview(scrollView)
        scrollView.addSubview(photoVm)
        scrollView.addSubview(msgVm)
        scrollView.addSubview(unincorporateVM)
        scrollView.addSubview(nextBtn)
        
        scrollView.contentSize = CGSize.init(width: 0, height: nextBtn.frame.maxY+kFitWidth(20))
        
        if AuthenCompanyMsgModel.shareInstance.settleWay == "LEGAL_PERSON"{
            if AuthenCompanyMsgModel.shareInstance.settleBankType == "1"{//对公
                self.msgVm.frame = CGRect.init(x: 0, y: self.msgVmOriginY, width: SCREEN_WIDHT, height: RegisterViewHeadTitleVM().selfHeight+RegisterInputVM().selfHeight*5)
                self.nextBtn.frame = CGRect.init(x: kFitWidth(31), y: self.msgVmOriginY+RegisterViewHeadTitleVM().selfHeight+RegisterInputVM().selfHeight*5+kFitWidth(40), width: kFitWidth(316), height: kFitWidth(48))
            }else{
                self.msgVm.frame = CGRect.init(x: 0, y: self.msgVmOriginY, width: SCREEN_WIDHT, height: RegisterViewHeadTitleVM().selfHeight+RegisterInputVM().selfHeight*6)
                self.nextBtn.frame = CGRect.init(x: kFitWidth(31), y: self.msgVmOriginY+RegisterViewHeadTitleVM().selfHeight+RegisterInputVM().selfHeight*6+kFitWidth(40), width: kFitWidth(316), height: kFitWidth(48))
            }
            
            self.refreshFrameForUnincorporateVM(isShowUnincorporate: false)
        }else{
            self.msgVm.frame = CGRect.init(x: 0, y: self.msgVmOriginY, width: SCREEN_WIDHT, height: RegisterViewHeadTitleVM().selfHeight+RegisterInputVM().selfHeight*2)
            self.nextBtn.frame = CGRect.init(x: kFitWidth(31), y: self.msgVmOriginY+RegisterViewHeadTitleVM().selfHeight+RegisterInputVM().selfHeight*2+kFitWidth(40), width: kFitWidth(316), height: kFitWidth(48))
            self.refreshFrameForUnincorporateVM(isShowUnincorporate: true)
        }
    }
    func udpateBankCardBackMsg(bankMsgDict:NSDictionary) {
        if AuthenCompanyMsgModel.shareInstance.settleWay == "LEGAL_PERSON"{
            msgVm.bankPrivateNameVm.textField.text = bankMsgDict["bank_name"]as? String ?? ""
    //        msgVm.bankNoVm.textField.text = bankMsgDict["bank_card_number"]as? String ?? ""
            let bankNo = bankMsgDict["bank_card_number"]as? String ?? ""
            msgVm.bankNoVm.textField.text = bankNo.replacingOccurrences(of: " ", with: "")
        }else{
            unincorporateVM.msgVm.bankPrivateBranchVm.setLabelContent(content: bankMsgDict["bank_name"]as? String ?? "")
            let bankNo = bankMsgDict["bank_card_number"]as? String ?? ""
            unincorporateVM.msgVm.bankNoVm.textField.text = bankNo.replacingOccurrences(of: " ", with: "")
        }
    }
    func updateUIForRefuse() {
        photoVm.updateUIForRefuse()
        msgVm.updateUIForRefuse()
        unincorporateVM.updateUIForRefuse()
    }
    func setReadMode() {
        nextBtn.isHidden = true
//        photoVm.isUserInteractionEnabled = false
        msgVm.isUserInteractionEnabled = false
        photoVm.setReadMode()
    }
    func refreshFrameForUnincorporateVM(isShowUnincorporate:Bool) {
        self.unincorporateVM.frame = CGRect.init(x: 0, y: self.msgVm.frame.maxY+kFitWidth(8), width: SCREEN_WIDHT, height: self.unincorporateVM.selfHeight)
        if isShowUnincorporate {
            unincorporateVM.isHidden = false
            nextBtn.frame = CGRect.init(x: kFitWidth(31), y: self.unincorporateVM.frame.maxY+kFitWidth(40), width: kFitWidth(316), height: kFitWidth(48))
        }else{
            unincorporateVM.isHidden = true
            nextBtn.frame = CGRect.init(x: kFitWidth(31), y: self.msgVm.frame.maxY+kFitWidth(40), width: kFitWidth(316), height: kFitWidth(48))
        }
        scrollView.contentSize = CGSize.init(width: 0, height: nextBtn.frame.maxY+kFitWidth(20))
    }
}

extension AuthenCompanySettleVM{
    @objc func nextAction() {
        if self.nextBlock != nil{
            self.nextBlock!()
        }
    }
    func getValue() {
//        AuthenCompanyMsgModel.shareInstance.settleName = msgVm.idNameVm.textField.text ?? ""
        AuthenCompanyMsgModel.shareInstance.settleBankNo = msgVm.bankNoVm.textField.text ?? ""
        AuthenCompanyMsgModel.shareInstance.settleBankNamePrivate = msgVm.bankPrivateNameVm.textField.text ?? ""
        AuthenCompanyMsgModel.shareInstance.settleBankNoPublic = msgVm.bankPublicNoVm.textField.text ?? ""
        AuthenCompanyMsgModel.shareInstance.settleBankNamePublic = msgVm.bankPublicNameVm.textField.text ?? ""
        AuthenCompanyMsgModel.shareInstance.settleBankPhone = msgVm.bankPhoneVm.textField.text ?? ""
        
        if AuthenCompanyMsgModel.shareInstance.settleWay == "UNINCORPORATE"{
            self.unincorporateVM.getValue()
        }
    }
    func checkValue() -> Bool {
        getValue()
        
        if AuthenCompanyMsgModel.shareInstance.settleBankType == "2" && AuthenCompanyMsgModel.shareInstance.photoImgUrlBankCardFront.count < 3 {
            MCToast.mc_text("请上传银行卡卡号照")
            return false
        }
        if AuthenCompanyMsgModel.shareInstance.settleBankType == "1" && AuthenCompanyMsgModel.shareInstance.photoImgUrlBankKaiHuXuKe.count < 3 {
            MCToast.mc_text("请上传银行开户许可证照")
            return false
        }
//        if AuthenCompanyMsgModel.shareInstance.photoImgUrlBankZhuanZhang.count < 3 {
//            MCToast.mc_text("请上传转账功能开通申请书")
//            return false
//        }
//        if AuthenCompanyMsgModel.shareInstance.photoImgUrlBankOther.count < 3 {
//            MCToast.mc_text("请上传转账意愿书及其他")
//            return false
//        }
//        if AuthenCompanyMsgModel.shareInstance.settleName.count < 2 {
//            MCToast.mc_text("请输入持卡人姓名")
//            return false
//        }
        if AuthenCompanyMsgModel.shareInstance.settleWay == "LEGAL_PERSON"{
            if AuthenCompanyMsgModel.shareInstance.settleBankNo.count < 8 {
                MCToast.mc_text("请输入银行卡号")
                return false
            }
            if AuthenCompanyMsgModel.shareInstance.settleBankType.count < 1 {
                MCToast.mc_text("请选择结算账户类型")
                return false
            }
            if !WHBaseViewVC().judgePhoneNumber(phoneNum: AuthenCompanyMsgModel.shareInstance.settleBankPhone){
                MCToast.mc_text("请输入正确的预留手机号")
                return false
            }
        }
        
//        if AuthenCompanyMsgModel.shareInstance.settleBankType == "1"{
//            if AuthenCompanyMsgModel.shareInstance.settleBankNoPublic.count < 5 {
//                MCToast.mc_text("请输入对公银行账户")
//                return false
//            }
//            if AuthenCompanyMsgModel.shareInstance.settleBankNamePublic.count < 2 {
//                MCToast.mc_text("请输入对公银行名")
//                return false
//            }
//            if AuthenCompanyMsgModel.shareInstance.settleBankBranchCodePublic.count < 2 {
//                MCToast.mc_text("请选择对公银行支行名")
//                return false
//            }
//        }else{
//            if AuthenCompanyMsgModel.shareInstance.settleBankNamePrivate.count < 2 {
//                MCToast.mc_text("请输入对私银行名称")
//                return false
//            }
//            if AuthenCompanyMsgModel.shareInstance.settleBankBranchNamePrivate.count < 2 {
//                MCToast.mc_text("请选择对私开户支行")
//                return false
//            }
//        }
        
        return true//self.unincorporateVM.checkValue()
    }
}


